//购物车对象
//id 菜单的id，name菜单名称，img 菜单图片，price菜单价格
var shopCart = {
	store : new Array(), //购物车中内容
	clickAddProduct : function() {
		var selectedProductId = $(event.target?event.target:event.srcElement).find("input")[0].value;
		shopCart.loadProduct(selectedProductId);
	},
	clickDeleteProductById : function(productId) {
		this.removeProductById(productId);
		this.showStoreHtml();
	},
	clickSubProductById : function(productId) {
		this.subProductToStoreById(productId);
		this.showStoreHtml();
	},
	clickAddProductById : function(productId) {
		this.addProductToStoreById(productId);
		this.showStoreHtml();
	},
	getProductFromStore : function(productId) {
		for(var i = 0; i < this.store.length; i++) {
			var tmp = this.store[i];
			if(tmp.id == productId) {
				return tmp;
			}
		}
	},
	loadProduct : function(selectedProductId) {
		//alert("loadProduct");
		//IE中为 srcElement,FF&CHROME target
		$.post(loadProductUrl, {
			'product.id' : selectedProductId
		}, function(data) {
			//alert(new Product(data.product))
			shopCart.addProductToStore(new Product(data.product));
			shopCart.showStoreHtml();
		}, "json");
		//这里返回的类型有：json,html,xml,text
	},
	subProductToStoreById : function(productId, updateCookie) {
		for(var i = 0; i < this.store.length; i++) {
			var tmp = this.store[i];
			if(tmp.id == productId) {
				if(tmp.piece <= 1) {
					this.removeProductById(productId)
				} else {
					tmp.piece--;
					if( typeof updateCookie == "undefined" || updateCookie) {
						this.addProductToCookie(tmp, 10);
					}
				}
			}
		}
	},
	addProductToStoreById : function(productId, updateCookie) {
		for(var i = 0; i < this.store.length; i++) {
			var tmp = this.store[i];
			if(tmp.id == productId) {
				tmp.piece++;
				if( typeof updateCookie == "undefined" || updateCookie) {
					this.addProductToCookie(tmp, 10);
				}
			}
		}
	},
	addProductToStore : function(product, updateCookie) {
		//alert("addProductToStore");
		var hasSelect = false;
		for(var i = 0; i < this.store.length; i++) {
			var tmp = this.store[i];
			if(this.store[i].id == product.id) {
				hasSelect = true;
				tmp.piece++;
				product.piece = tmp.piece;
			}
		}
		if(!hasSelect) {
			this.store.push(product);
		}
		//1.查询cookie中是否存在，2.如果存在则修改，如果不存在则添加；
		//alert("addToShopCart");
		//alert(product)
		if( typeof updateCookie == "undefined" || updateCookie) {
			this.addProductToCookie(product, 10);
		}
	},
	removeProductById : function(productId) {
		if(isNaN(productId)) {
			return false;
		}
		for(var i = 0, n = 0; i < this.store.length; i++) {
			if(this.store[i].id != productId) {
				this.store[n++] = this.store[i];
			}
		}
		this.store.length -= 1;
		//alert(this.store)
		this.removeProductToCookieById(productId);
	},
	removeProductByIndex : function(index) {
		if(isNaN(index) || index > this.store.length) {
			return false;
		}
		for(var i = 0, n = 0; i < this.store.length; i++) {
			if(this.store[i].id != this.store[index].id) {
				this.store[n++] = this.store[i];
			}
			this.store.length -= 1;
		}
		//删除cookie中的数据
	},
	addProductToCookie : function(product, expireHours) {
		var cookieString = "product" + escape(product.id) + "=" + product.piece
		if(expireHours > 0) {
			var date = new Date();
			date.setTime(date.getTime() + expireHours * 3600 * 1000);
			cookieString = cookieString + "; expires=" + date.toGMTString();
		}
		//alert("addProductToCookie:" + cookieString);
		document.cookie = cookieString;
	},
	removeProductToCookieById : function(productId) {
		var cookieString = "product" + escape(productId) + "=" + 0;
		var date = new Date();
		date.setTime(date.getTime() - 10 * 3600 * 1000);
		cookieString = cookieString + "; expires=" + date.toGMTString();
		document.cookie = cookieString;
	},
	showStoreHtml : function() {
		//alert(this.store.toShowProduct())
		$("#shopCartUL").html(this.store.toShowProduct());
	},
	loadCookie : function() {
		this.store=new Array;
		//读取COOKIE中内容
		var strCookie = document.cookie;
		var arrayCookie = strCookie.split(";");
		var ids = "";
		var productsInCookie = new Array();
		for(var i = 0; i < arrayCookie.length; i++) {
			var str = arrayCookie[i].Trim();
			if(str.indexOf("product") != -1) {
				//alert(str)
				//alert(str.substring("product".length, "product".length + 1));
				var product = new Product();
				var id = str.substring("product".length, "product".length + 1);
				var piece = str.substring(str.indexOf("=") + 1);
				product.id = id;
				product.piece = piece;
				productsInCookie.push(product);
				ids += id + ";";
				ids += id + ";";
			}
		}
		//productInCookie 中数据操作，存入购物车
		//alert("productsInCookie:" + productsInCookie);
		$.post(loadProductsByIdsUrl, {
			'ids' : ids
		}, function(data) {
			var products = data.products;
			//alert(JSON.stringify(data.products))
			for(var i = 0; i < products.length; i++) {
				shopCart.addProductToStore(new Product(products[i]), false);
			}
			//productsInCookie 中 份数处理
			for(var i = 0; i < shopCart.store.length; i++) {
				var tmp = shopCart.store[i];
				for(var j = 0; j < productsInCookie.length; j++) {
					if(productsInCookie[j].id == tmp.id) {
						tmp.piece = productsInCookie[j].piece;
					}
				}
			}
			//alert("shopCartStore:"+shopCart.store);
			shopCart.showStoreHtml();
			//alert(new Product(data.product))
		}, "json");
		// $.post(loadProductsByIdsUrl, {
		// 'ids' : ids
		// }, function(data) {
		// //alert(JSON.stringify(data.products))
		// //alert(new Product(data.product))
		// }, "json");
		//需要装载 shopstar.store
		//显示在页面中
	}
}

function Product(jsonProduct) {
	if( typeof (jsonProduct) == "undefined") {
	} else {
		this.id = jsonProduct.id;
		this.name = jsonProduct.name;
		this.price = jsonProduct.price;
		//默认为一个，可以修改
		this.piece = 1;
	}
}

Product.prototype.toString = function() {
	var str = "";
	if(this.id) {
		str += "|id:" + this.id;
	}
	if(this.name) {
		str += "|name:" + this.name;
	}
	if(this.price) {
		str += "|price:" + this.price;
	}
	if(this.piece) {
		str += "|piece:" + this.piece;
	}
	return str;
}
//数组删除指定index元素方法
Array.prototype.removeProduct = function(index) {
	if(isNaN(index) || index > this.length) {
		return false;
	}
	for(var i = 0, n = 0; i < this.length; i++) {
		if(this[i] != this[index]) {
			this[n++] = this[i];
		}
	}
	this.length -= 1;
}
Array.prototype.toShowProduct = function() {
	var result = "";
	for(var i = 0; i < this.length; i++) {
		result += "<li>";
		result += this[i].name + "<br>";
		result += "<b>￥" + this[i].price * this[i].piece + "(数量:" + this[i].piece + ")</b>";
		result += "<span class='del' onclick=shopCart.clickDeleteProductById(" + this[i].id + ")></span>";
		result += "<span class='sub' onclick=shopCart.clickSubProductById(" + this[i].id + ")></span>";
		result += "<span class='add'  onclick=shopCart.clickAddProductById(" + this[i].id + ")></span>";
		result += "</li>";
	}
	return result;
}
//出处:网上搜集
//made by yaosansi 2005-12-02
//For more visit http://www.yaosansi.com
// Trim() , Ltrim() , RTrim()
//String 扩展
String.prototype.Trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.LTrim = function() {
	return this.replace(/(^\s*)/g, "");
}

String.prototype.RTrim = function() {
	return this.replace(/(\s*$)/g, "");
}